Completed
Push — phpunit ( 7f2080...75f541 )
by Marcos
14:28 queued 10:45
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 221

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 6
c 6
b 0
f 0
nc 25
nop 11
dl 0
loc 221
rs 8.1463

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
(function () {
2
	'use strict';
3
4
5
	/**
6
	 * @ngdoc function
7
	 * @name passmanApp.controller:SettingsCtrl
8
	 * @description
9
	 * # SettingsCtrl
10
	 * Controller of the passmanApp
11
	 */
12
	angular.module('passmanApp')
13
		.controller('SettingsCtrl', ['$scope', '$rootScope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$http', 'EncryptService', 'NotificationService', '$sce',
14
			function ($scope, $rootScope, SettingsService, VaultService, CredentialService, $location, $routeParams, $http, EncryptService, NotificationService, $sce) {
15
				$scope.vault_settings = {};
16
				$scope.new_vault_name = '';
17
				$scope.active_vault = VaultService.getActiveVault();
18
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
19
					if (!$scope.active_vault) {
20
						$location.path('/');
21
						return;
22
					}
23
				} else {
24
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
25
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
26
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
27
						VaultService.setActiveVault(_vault);
28
						$scope.active_vault = _vault;
29
					}
30
				}
31
32
				VaultService.getVault($scope.active_vault).then(function (vault) {
33
					vault.vaultKey = VaultService.getActiveVault().vaultKey;
34
					delete vault.credentials;
35
					VaultService.setActiveVault(vault);
36
					$scope.vault_settings = vault.vault_settings;
37
					if(!$scope.vault_settings.hasOwnProperty('pwSettings')){
38
						$scope.vault_settings.pwSettings = {
39
							'length': 12,
40
							'useUppercase': true,
41
							'useLowercase': true,
42
							'useDigits': true,
43
							'useSpecialChars': true,
44
							'minimumDigitCount': 3,
45
							'avoidAmbiguousCharacters': false,
46
							'requireEveryCharType': true,
47
							'generateOnCreate': true
48
						};
49
					}
50
				});
51
52
53
54
				var http = location.protocol, slashes = http.concat("//"), host = slashes.concat(window.location.hostname), complete = host + location.pathname;
55
				$scope.bookmarklet = $sce.trustAsHtml("<a class=\"button\" href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,e=c(document.title),d=a.open('" + complete + "bookmarklet?url='+c(b.location)+'&title='+e,'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=750px,width=475px,resizable=0,alwaysRaised=1');a.setTimeout(function(){d.focus()},300);})();\">Save in passman</a>");
56
57
58
				$scope.saveVaultSettings = function () {
59
					var _vault = $scope.active_vault;
60
					_vault.name = $scope.new_vault_name;
61
					_vault.vault_settings = angular.copy($scope.vault_settings);
62
					VaultService.updateVault(_vault).then(function () {
63
						//VaultService.setActiveVault(_vault);
64
						$scope.active_vault.name = angular.copy(_vault.name);
65
						NotificationService.showNotification('Settings saved', 5000);
66
					});
67
				};
68
69
70
				$scope.tabs = [
71
					{
72
						title: 'General settings',
73
						url: 'views/partials/forms/settings/general_settings.html'
74
					},
75
					{
76
						title: 'Password Audit',
77
						url: 'views/partials/forms/settings/tool.html'
78
79
					},
80
					{
81
						title: 'Password settings',
82
						url: 'views/partials/forms/settings/password_settings.html'
83
84
					},
85
					{
86
						title: 'Import credentials',
87
						url: 'views/partials/forms/settings/import.html'
88
89
					},
90
					{
91
						title: 'Export credentials',
92
						url: 'views/partials/forms/settings/export.html'
93
94
					},
95
					{
96
						title: 'Sharing',
97
						url: 'views/partials/forms/settings/sharing.html'
98
					}
99
				];
100
101
				$scope.currentTab = $scope.tabs[0];
102
103
				$scope.onClickTab = function (tab) {
104
					$scope.currentTab = tab;
105
				};
106
107
				$scope.isActiveTab = function (tab) {
108
					return tab.url === $scope.currentTab.url;
109
				};
110
111
				var getPassmanVersion = function () {
112
					var url = OC.generateUrl('apps/passman/api/internal/version');
113
					$http.get(url).then(function (result) {
114
						$scope.passman_version = result.data.version;
115
					});
116
				};
117
				getPassmanVersion();
118
119
				$scope.$watch(function () {
120
					return VaultService.getActiveVault();
121
				}, function (vault) {
122
					if (vault) {
123
						$scope.active_vault = vault;
124
					}
125
				});
126
127
				$rootScope.$on('logout', function () {
128
					$scope.selectedVault = false;
129
				});
130
				$scope.startScan = function (minStrength) {
131
					VaultService.getVault($scope.active_vault).then(function (vault) {
132
						var results = [];
133
						for (var i = 0; i < vault.credentials.length; i++) {
134
							var c = angular.copy(vault.credentials[i]);
135
							if (c.password && c.hidden === 0) {
136
								c = CredentialService.decryptCredential(c);
137
								if (c.password) {
138
									var zxcvbn_result = zxcvbn(c.password);
139
									if (zxcvbn_result.score <= minStrength) {
140
										results.push({
141
											credential_id: c.credential_id,
142
											label: c.label,
143
											password: c.password,
144
											password_zxcvbn_result: zxcvbn_result
145
										});
146
									}
147
								}
148
149
							}
150
							//@todo loop custom fields (if any and check secret fields
151
						}
152
						$scope.scan_result = results;
153
					});
154
				};
155
156
157
				$scope.cur_state = {};
158
159
160
				$scope.$on("$locationChangeStart", function(event) {
161
					if($scope.change_pw){
162
						if($scope.change_pw.total > 0 && $scope.change_pw.done < $scope.change_pw.total){
163
							if(!confirm("Are you sure you want to leave?\nThis will corrupt all your credentials")){
164
								event.preventDefault();
165
							}
166
						}
167
					}
168
				});
169
170
171
				$scope.changeVaultPassword = function (oldVaultPass, newVaultPass, newVaultPass2) {
172
					if (oldVaultPass !== VaultService.getActiveVault().vaultKey) {
173
						$scope.error = 'Your old password is incorrect!';
174
						return;
175
					}
176
					if (newVaultPass !== newVaultPass2) {
177
						$scope.error = 'New passwords do not match!';
178
						return;
179
					}
180
					VaultService.getVault($scope.active_vault).then(function (vault) {
181
						var _selected_credentials = [];
182
						if (vault.credentials.length === 0) {
183
							$location.path('/');
184
						}
185
						for (var i = 0; i < vault.credentials.length; i++) {
186
							var _credential = vault.credentials[i];
187
							if (_credential.shared_key === null || _credential.shared_key === '') {
188
								_selected_credentials.push(_credential);
189
							}
190
						}
191
						$scope.change_pw = {
192
							percent: 0,
193
							done: 0,
194
							total: _selected_credentials.length
195
						};
196
						var changeCredential = function (index, oldVaultPass, newVaultPass) {
197
							CredentialService.reencryptCredential(_selected_credentials[index].guid, oldVaultPass, newVaultPass).progress(function (data) {
198
								$scope.cur_state = data;
199
							}).then(function () {
200
								var percent = index / _selected_credentials.length * 100;
201
								$scope.change_pw = {
202
									percent: percent,
203
									done: index + 1,
204
									total: _selected_credentials.length
205
								};
206
								if (index < _selected_credentials.length - 1) {
207
									changeCredential(index + 1, oldVaultPass, newVaultPass);
208
								} else {
209
									vault.private_sharing_key = EncryptService.decryptString(angular.copy(vault.private_sharing_key), oldVaultPass);
210
									vault.private_sharing_key = EncryptService.encryptString(vault.private_sharing_key, newVaultPass);
211
									VaultService.updateSharingKeys(vault).then(function () {
212
										$rootScope.$broadcast('logout');
213
										NotificationService.showNotification('Please login with your new vault password', 5000);
214
									});
215
								}
216
							});
217
						};
218
						changeCredential(0, VaultService.getActiveVault().vaultKey, newVaultPass);
219
220
					});
221
				};
222
223
				$rootScope.$on('logout', function () {
224
					$scope.active_vault = null;
225
					VaultService.setActiveVault(null);
226
					$location.path('/');
227
228
				});
229
230
				$scope.cancel = function () {
231
					$location.path('/vault/' + $routeParams.vault_id);
232
				};
233
234
			}]);
235
236
}());